package com.pvdnc.worldmagisk;

import androidx.appcompat.app.AppCompatActivity;
import cn.trinea.android.common.util.ShellUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.pvdnc.worldmagisk.shell.client.ShellClient;
import com.pvdnc.worldmagisk.shell.server.ShellServer;

import java.io.IOException;
import java.io.PipedOutputStream;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private ShellClient mShellClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        final Handler mainHandler=new Handler(Looper.getMainLooper());
        final TextView lblResult=findViewById(R.id.lblResult);
        Button cmdExecute=findViewById(R.id.cmdExecute);
        cmdExecute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1 = "cd /data/local/tmp";
                String s2 = "app_process -Djava.class.path=/data/local/tmp/ShellServer.dex /data/local/tmp com.pvdnc.worldmagisk.shell.server.ShellServer";
                final String[] arr = {s1, s2};
                ShellUtils.execCommand(arr, true);
            }
        });

        Button cmdConnect=findViewById(R.id.cmdConnect);
        cmdConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mShellClient!=null)
                    return;
                mShellClient=new ShellClient(ShellServer.DEFAULT_PORT);
                mShellClient.doConnect();
            }
        });
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
